package cn.itsource.system.controllor;

import cn.itsource.system.domain.Menu;
import cn.itsource.system.query.MenuQuery;
import cn.itsource.system.service.MenuService;
import cn.itsource.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    
    @Autowired
    private MenuService menuService;

    /**
     * 新增或修改
     * @param m
     * @return
     */
    @PostMapping
    public AjaxResult save(@RequestBody Menu m){
        if (m.getId()==null){
            menuService.insert(m);
            return AjaxResult.ok();
        }else {
            menuService.updateByPrimaryKeySelective(m);
            return AjaxResult.ok();
        }
    }
    



    /**
     * 分页查询
     * @param menuQuery
     * @return
     */
    @PatchMapping
    public AjaxResult pageInfo(@RequestBody MenuQuery menuQuery){
        try {
            return AjaxResult.ok(menuService.pageInfo(menuQuery));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 批量删除
     * @param m
     * @return
     */
    @PatchMapping("/batchDeletion")
    public AjaxResult batchDeletion(@RequestBody List<Menu> m){
        try {
            menuService.deleteAll(m);
            return AjaxResult.ok();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            menuService.deleteByPrimaryKey(id);
            return AjaxResult.ok();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 树形展示
     * @return
     */
    @PatchMapping("/gettree")
    public AjaxResult result(){
        try {
            return AjaxResult.ok(menuService.gettree());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
}
